package com.powernode.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.Notice;
import com.powernode.model.Result;
import com.powernode.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author: Tong Ziyu
 * @Date: 2025/4/13 22:47
 */
@RestController
@RequestMapping("shop/notice")
@Api(tags = "通知管理")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    /**
     * 分页查询通知
     * @return
     */
    @ApiOperation("分页查询通知")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('shop:notice:page')")
    public Result<Page> pageNotice(
            @RequestParam(value = "current") Long current,
            @RequestParam(value = "size") Long size,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "status", required = false) Long status,
            @RequestParam(value = "isTop", required = false) Long isTop


    ){
        Page page = new Page<Notice>(current, size);

        page = noticeService.page(page,
                new LambdaQueryWrapper<Notice>()
                        .like(StringUtils.hasText(title), Notice::getTitle, title)
                        .eq(status != null, Notice::getStatus, status)
                        .eq(isTop != null, Notice::getIsTop, isTop)
                );
        return Result.success(page);
    }

    /**
     * 新增通知
     * @return
     */
    @ApiOperation("新增通知")
    @PostMapping()
    @PreAuthorize("hasAuthority('shop:notice:save')")
    public Result<String> saveNotice(@RequestBody Notice notice){
        return Result.handle(noticeService.saveNotice(notice));
    }

    /**
     * 根据通知id查看通知
     * @return
     */
    @ApiOperation("根据通知id查看通知")
    @GetMapping("/info/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:info')")
    public Result<Notice> getNoticeById(@PathVariable("noticeId") Long noticeId){
        return Result.success(noticeService.getById(noticeId));
    }

    /**
     * 删除通知
     * @return
     */
    @ApiOperation("根据id删除通知")
    @DeleteMapping("/{noticeId}")
    @PreAuthorize("hasAuthority('shop:notice:delete')")
    public Result<String> deleteNoticeById(@PathVariable("noticeId")Long noticeId){
        return Result.handle(noticeService.removeById(noticeId));
    }

    /**
     * 修改通知
     * @return
     */
    @ApiOperation("修改通知")
    @PutMapping()
    @PreAuthorize("hasAuthority('shop:notice:update')")
    public Result<String> updateNotice(@RequestBody Notice notice){
        return Result.handle(noticeService.updateById(notice));
    }

}
